<template>
  <div class="plan-list full-height bg-white page-content content">
    <yz-condition-filter
          v-model="queryParams"
          @resetSearch="resetSearch"
          @onSubmit="onSubmit"
          class="padding-t-20"
        >
        <select-tooltip
            v-model="queryParams.workshopId"
            :multiple="false"
            isShowAll
            :options="workshopslist"
            msg="所在车间"
          ></select-tooltip>
        <select-tooltip
            v-model="queryParams.deviceType"
            :multiple="false"
            isShowAll
            :options="deviceTypesList"
            msg="设备类型"
          ></select-tooltip>
            <!-- <fuzzy-search
              v-model.trim="queryParams.productId"
              :fuzzyParams="fuzzyParams"
              fuzzyValue="assetNumber"
              :fuzzyObj="['name', 'id']"
              isFilter
              class="margin-r-20"
              isShowAll
              title="设备编号"
              :fuzzyFunc="fuzzyFunc"
            ></fuzzy-search> -->
        <select-tooltip
            v-model="queryParams.maintenanceType"
            :multiple="false"
            isShowAll
            @input="onSelectProgram"
            :options="maintenanceTypeOptions"
            msg="保养类型"
          ></select-tooltip>
        <select-tooltip
            v-model="queryParams.programId"
            :multiple="false"
            isShowAll
            :options="maintenaceProgramsList"
            msg="项目名称"
          ></select-tooltip>
        <select-tooltip
            v-model="queryParams.recurRuleFreq"
            :multiple="false"
            isShowAll
            :options="maintenanceCycleOptions"
            msg="保养周期"
          ></select-tooltip>
        <yz-input
              v-model.trim="queryParams.scheduleNo"
              class="q-mr-md"
              :maxlength="50"
              title="计划编号"
         ></yz-input>
        </yz-condition-filter>
      <div class="form-inline bottom justify-between padding-b-20">
          <div class="left box">
            <q-btn class="btn margin-r-10"
                   outline
                   color="primary"
                   @click="onCreatPlanList(null)">新增</q-btn>
          </div>
      </div>
        <q-table
          :data="tableData"
          :columns="columns"
          row-key="id"
          class="myTable scroll stick-last-col"
          :loading="isTableLoading"
          :pagination.sync="pagination"
          :hide-bottom="tableData.length > 0"
          no-data-label="暂无数据"
          color="primary"
        >
      <template v-slot:body-cell-maintenanceType="props">
        <q-td>
          {{ $getEnum(Enums.MAINTENANCE_TYPE, props.row.maintenanceType).desc | toHorizontalLine }}
        </q-td>
      </template>
      <template v-slot:body-cell-nextMADate="props">
        <q-td>
          {{props.row.nextMADate | toDate  }}
        </q-td>
      </template>
      <template v-slot:body-cell-recurRuleFreq="props">
        <q-td>
          {{ $getEnum(Enums.MAINTENANCE_CYCLE, props.row.recurRuleFreq).desc | toHorizontalLine }}
        </q-td>
      </template>
      <template v-slot:body-cell-deviceQuantity="{ row }">
           <q-td>
            <q-btn flat
                   color="primary"
                   :disable="row.deviceQuantity === 0"
                   @click="onDeviceDetail(row)">{{row.deviceQuantity}}</q-btn>
          </q-td>
      </template>
      <template v-slot:header-cell-createTime="props">
        <q-th :props="props"
              class="sortable sorted-icon"
              @click="onSeletByTime">
            {{props.col.label}}
            <q-icon class="q-table__sort-icon"  :name="createTimeAsc ? 'arrow_upward' : 'iconfont iconjiantoupaixu'"></q-icon>
        </q-th>
      </template>
      <template v-slot:body-cell-createTime="props">
        <q-td>
          {{ props.row.createTime | toTime }}
        </q-td>
      </template>
      <template v-slot:body-cell-operations="props">
        <q-td class="operations" style="width: 100px">
          <div class="btn-group text-center">
            <table-btn btnType="detail" @click="onDetailPlanList(props.row)"/>
            <table-btn btnType="edit" @click="onCreatPlanList(props.row)"/>
            <table-btn btnType="delete" @click="onDeleteRowById(props.row.id)"/>
            <q-toggle
                v-model="props.row.enableFlag"
                checked-icon="iconfont icontubiao-49"
                unchecked-icon="iconfont icontubiao-48"
                true-value="1"
                false-value="0"
                color="positive"
                @input="onStatusToggle(props.row)"
              >
            </q-toggle>
          </div>
        </q-td>
      </template>
      <template v-slot:no-data>
          <div class="full-width row flex-center font-size-14" style="color: #999">
            暂无数据
          </div>
      </template>
    </q-table>
    <yz-pagination v-model="pagination" @onSearch="getList"/>
    <device-dialog :show="isShowDeviceDialog" @close="isShowDeviceDialog = false" :scheduleId="scheduleId"/>
  </div>
</template>

<script>
import _ from 'lodash'
import deviceDialog from './common/device-dialog'
import MAINTENANCEMIXIN from '../maintenance-mixin'
import { getMaintenaceProgramsList } from 'src/boot/maintenance/maintenance-item'
import { getScheduleList, deleteScheduleById, editMaintenanceEnabledStatus } from 'src/boot/maintenance/maintenance-plan'
export default {
  name: 'plan-list',
  mixins: [MAINTENANCEMIXIN],
  components: { deviceDialog },
  data () {
    return {
      queryParams: {
        maintenanceType: 'all',
        workshopId: 'all',
        deviceType: 'all',
        programId: 'all',
        recurRuleFreq: 'all',
        orderBy: null,
        scheduleNo: ''
      },
      isTableLoading: false,
      isShowDeviceDialog: false,
      tableData: [],
      scheduleId: '', // 设备弹窗id
      columns: [
        { name: 'scheduleNo', label: '计划编号', align: 'left', field: 'scheduleNo' },
        { name: 'deviceQuantity', label: '设备数量', align: 'left', field: 'deviceQuantity' },
        { name: 'deviceName', label: '设备类型', align: 'left', field: 'deviceName' },
        { name: 'maintenanceType', label: '保养类型', align: 'left', field: 'maintenanceType' },
        { name: 'programName', label: '项目名称', align: 'left', field: 'programName' },
        { name: 'nextMADate', label: '下次保养日期', align: 'left', field: 'nextMADate' },
        { name: 'recurRuleFreq', label: '循环周期', align: 'left', field: 'recurRuleFreq' },
        { name: 'createTime', label: '创建时间', align: 'left', field: 'createTime' },
        {
          name: 'operations',
          field: 'operations',
          label: '操作',
          align: 'center'
        }
      ],
      createTimeAsc: false,
      maintenaceProgramsList: [] // 保养项目内容
    }
  },
  computed: {
    //  保养周期枚举
    maintenanceCycleOptions () {
      return this.$getEnum(this.Enums.MAINTENANCE_CYCLE)
    },
    //  保养类型枚举
    maintenanceTypeOptions () {
      return this.$getEnum(this.Enums.MAINTENANCE_TYPE)
    }
  },
  beforeRouteEnter (to, from, next) {
    to.name === 'maintenancePlan' && next(vm => {
      vm.getList()
    })
  },
  methods: {
    changeSelectVal (order) {
      this.queryParams.scheduleNo = order
      this.onSubmit()
    },
    onCreatPlanList (row) {
      const link = _.isNil(row) ? 'add' : 'edit'
      const query = _.isNil(row) ? {} : { id: row.id }
      this.$router.push({
        path: `/main/repair/maintenance-plan/${link}-maintenance-plan`,
        query
      })
    },
    onDetailPlanList (row) {
      this.$router.push({
        path: '/main/repair/maintenance-plan/detail-maintenance-plan',
        query: {
          id: row.id
        }
      })
    },
    onDeleteRowById (id) {
      this.$utils.showMessage('icontubiao-28', '是否确认删除', () => {
        deleteScheduleById(id).then((res) => {
          this.$message.success('删除成功')
          this.getList()
        }).catch((err) => {
          console.log(err)
        })
      }, true)
    },
    onSeletByTime () {
      this.createTimeAsc = !this.createTimeAsc
      this.queryParams.orderBy = this.createTimeAsc ? 'createTime' : null
      this.getList()
    },
    onDeviceDetail (row) {
      this.scheduleId = row.id
      this.isShowDeviceDialog = true
    },
    onSubmit () {
      this.pagination.pageNum = 1
      this.getList()
    },
    async onStatusToggle (row) {
      const member = row.enableFlag === '1' ? 'ENABLED' : 'DISABLED'
      this.$utils.showMessage('icontubiao-28', row.enableFlag === '1' ? '需要重新设置计划开始时间' : '确认禁用吗?', () => {
        editMaintenanceEnabledStatus({ id: row.id, status: member }).then((res) => {
          this.$message.success('操作成功')
          this.getList()
        }).catch((err) => {
          console.log(err)
          this.getList()
        })
      }, true, () => {
        this.getList()
      })
    },
    // 改变保养类型 获取最新项目名称
    onSelectProgram () {
      this.queryParams.programId = 'all'
      this.getMaintenaceProgramsList()
    },
    // 获取保养项目
    getMaintenaceProgramsList () {
      const params = {
        maintenanceType: this.queryParams.maintenanceType === 'all' ? null : this.queryParams.maintenanceType,
        sort: 0
      }
      getMaintenaceProgramsList(params).then(res => {
        this.maintenaceProgramsList = res.data.list.map(v => {
          const item = v
          item.label = v.name
          item.value = v.id
          return item
        })
      }).catch(err => {
        this.maintenaceProgramsList = []
        console.log(err)
      })
    },
    // 获取数据
    getList () {
      const params = {
        ...this.pagination,
        ...this.queryParams
      }
      this.isTableLoading = true
      getScheduleList(params).then((res) => {
        const ret = res.data
        this.tableData = ret.list.map(v => {
          const item = v
          item.deviceName = _.get(this.deviceTypesList.find(i => i.value === v.deviceType), 'label', '--')
          return item
        })
        this.pagination.rowsNumber = ret.total
        this.isTableLoading = false
      }).catch((err) => {
        console.log(err)
        this.isTableLoading = false
      })
    }
  },
  mounted () {
    this.getMaintenaceProgramsList()
  }
}
</script>
<style scoped lang="scss">
</style>
